package kit.views;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

import kit.persistency.KitDB;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class AnswerSelectionPanel extends KitPanel {
	private JComboBox comboBox;

	/**
	 * Create the panel.
	 */
	public AnswerSelectionPanel() {
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		add(panel, BorderLayout.SOUTH);
		
		JButton button = new JButton("Salva");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(AnswerSelectionPanel.this.getDomainComboBox().getSelectedIndex() == -1)
					return;
				KitFrame frame = (KitFrame) SwingUtilities.getRoot(AnswerSelectionPanel.this);
				String question = (String) frame.getQuestionList().getSelectedValue();
				String answer = (String) AnswerSelectionPanel.this.getDomainComboBox().getSelectedItem();
				KitDB db = new KitDB();
				if(!db.insertAnswer(question, answer)){
					AnswerSelectionPanel.this.showErrorDialog("La risposta potrebbe già essere presente.\n", frame);
				}
				frame.getCenterPanel().removeAll();
				frame.repaint();
			}
		});
		button.setHorizontalTextPosition(SwingConstants.CENTER);
		panel.add(button);
		
		JPanel panel_1 = new JPanel();
		add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
		JLabel lblSelezionaUnaRisposta = new JLabel("Seleziona una risposta");
		panel_1.add(lblSelezionaUnaRisposta, "2, 2, right, default");
		
		comboBox = new JComboBox();
		comboBox.setMaximumRowCount(25);
		panel_1.add(comboBox, "4, 2, fill, default");

	}

	protected JComboBox getDomainComboBox() {
		return comboBox;
	}
}
